// pages/hotel/hotel.js
Page({
  data: {
    keyword: '', // 搜索关键词
    hotels: [], // 酒店列表
    loading: false // 加载状态
  },

  onLoad: function (options) {
    this.loadHotels();
  },
  
  // 加载酒店列表
  loadHotels: function() {
    this.setData({ loading: true });
    
    wx.mRequest({
      url: '/api/hotel/all',
      method: 'GET',
      success: (res) => {
        this.setData({
          hotels: res.data || [],
          loading: false
        });
      },
      fail: () => {
        this.setData({ loading: false });
        wx.showToast({
          title: '加载失败，请重试',
          icon: 'none'
        });
      }
    });
  },
  
  // 关键词输入事件
  onKeywordInput: function(e) {
    this.setData({
      keyword: e.detail.value
    });
  },
  
  // 搜索按钮点击事件
  onSearch: function() {
    if (!this.data.keyword.trim()) {
      this.loadHotels();
      return;
    }
    
    this.setData({ loading: true });
    
    wx.mRequest({
      url: '/api/hotel/search',
      method: 'GET',
      data: {
        name: this.data.keyword
      },
      success: (res) => {
        this.setData({
          hotels: res.data || [],
          loading: false
        });
      },
      fail: () => {
        this.setData({ loading: false });
        wx.showToast({
          title: '搜索失败，请重试',
          icon: 'none'
        });
      }
    });
  },
  
  // 跳转到酒店详情页
  goToDetail: function(e) {
    const hotelId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/hotel/detail/detail?id=' + hotelId
    });
  },
  
  // 下拉刷新
  onPullDownRefresh: function() {
    this.loadHotels();
    wx.stopPullDownRefresh();
  }
}) 